package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.TrainingResultMapper;
import com.think.teaching.model.TrainingResult;
import com.think.teaching.service.TrainingResultService;

import java.util.Map;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class TrainingResultServiceImpl extends BaseServiceImpl<TrainingResultMapper, TrainingResult>
		implements TrainingResultService {

	@Override
	public TrainingResult AddTrainingResult(TrainingResult trainingResult) {

		if (this.save(trainingResult)) {
			return trainingResult;
		}
		return null;
	}

	@Override
	public TrainingResult UpdateTrainingResult(TrainingResult trainingResult) {
		if (this.updateById(trainingResult)) {
			return trainingResult;
		}
		return null;
	}

	@Override
	public TrainingResult GetTrainingResult(Integer id) {
		QueryWrapper<TrainingResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public IPage<TrainingResult> ListTrainingResult(Integer page, Integer count, Boolean withoutBan) {
		QueryWrapper<TrainingResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<TrainingResult> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public IPage<TrainingResult> ListTrainingResultWithUser(Integer userId, Integer page, Integer count,
			Boolean withoutBan) {
		QueryWrapper<TrainingResult> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).ne("userId", userId);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<TrainingResult> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public TrainingResult SetTrainingResultState(Integer id, Integer state) {
		UpdateWrapper<TrainingResult> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	public int GetOutstandingCount(Integer course) {
		// TODO Auto-generated method stub
		return baseMapper.GetOutstandingCount(course);
	}

	public int GetGoodCount(Integer course) {
		// TODO Auto-generated method stub
		return baseMapper.GetGoodCount(course);
	}

	public IPage<Map<String, Object>>  ListTrainingResult(Integer count, Integer page, Integer course) {
		// TODO Auto-generated method stub
		return baseMapper.ListTrainingResult(new Page<Map<String,Object>>(count, page),count);
	}

}
